package name.gauravmadan.mdg.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import name.gauravmadan.mdg.pim.Component;
import name.gauravmadan.mdg.service.ModelDataService;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class ComponentListController extends MultiActionController{
	private ModelDataService service;
	
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = null;
		String parentApplicationId = request.getParameter("parentApplicationId");
		if (parentApplicationId != null && !"".equals(parentApplicationId)) {
			List<Component> componentList = service.getComponentListByAppId(new Integer(parentApplicationId));
			mav = new ModelAndView("component-list");
			mav.addObject("componentList", componentList);
		}
		else {
			// Return error here
		}
		return mav;
	}

	public ModelAndView showFormNew(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = null;
		Component comp = new Component();
		String parentApplicationId = request.getParameter("parentApplicationId");
		if (parentApplicationId != null && !"".equals(parentApplicationId)){
			comp.setParentApplicationId(new Integer(parentApplicationId));
			mav = new ModelAndView("component-form");
			mav.addObject("component", comp);
		}
		else {
			// Return error here
		}
		return mav;
	}
	public ModelAndView showFormEdit(HttpServletRequest request, HttpServletResponse response)
	{
		String selectedComponentId = request.getParameter("selectedComponentId");
		// Load Component Object based on selected component id
		System.out.println("SELECTED COMPONENT ID IS: " + selectedComponentId);
		ModelAndView mav = new ModelAndView("component-form");
		Component comp = null;
		if (selectedComponentId != null && !"".equals(selectedComponentId)) {
			comp = service.getComponentById(new Integer(selectedComponentId));
		}
		else {
			// Return error here
		}
		mav.addObject("component", comp);
		return mav;
	}
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response)
	{
		String selectedComponentId = request.getParameter("selectedComponentId");
		if (selectedComponentId != null && !"".equals(selectedComponentId)) {
			service.deleteComponentById(new Integer(selectedComponentId));
		}
//		ModelAndView mav = null;
//		String parentApplicationId = request.getParameter("parentApplicationId");
//		if (parentApplicationId != null && !"".equals(parentApplicationId)){
//			List<Component> componentList = service.getComponentListByAppId(new Integer(parentApplicationId));
//			mav = new ModelAndView("component-list");
//			mav.addObject("componentList", componentList);
//		}else {
//			// Return error here
//		}
//		return mav;
		ModelAndView mav = new ModelAndView("redirect:/component-list.html?method=list");
		mav.addObject("parentApplicationId", request.getParameter("parentApplicationId"));
		return mav;
	}
	public ModelAndView showClasses(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("redirect:/class-list.html?method=list");
		mav.addObject("parentComponentId", request.getParameter("selectedComponentId"));
		return mav;
	}
	
	public ModelDataService getService() {
		return service;
	}
	public void setService(ModelDataService service) {
		this.service = service;
	}
}
